package org.wisdom.ab.domain.staff.entity;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.persistence.*;

import org.springframework.core.annotation.Order;
import org.springframework.data.domain.DomainEvents;
import org.wisdom.data.jpa.annotation.SerialNumber;
import org.wisdom.data.jpa.entity.BaseEntity;

import lombok.*;
import lombok.extern.slf4j.Slf4j;

/**
 * 成员
 *
 * @author 张智研
 * @date 2022/05/16 08:16
 **/
@SuppressWarnings("JpaDataSourceORMInspection")
@EqualsAndHashCode(callSuper = true)
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true)
@Slf4j
@NamedEntityGraph(name = "staffWithDepartment",
        attributeNodes = {@NamedAttributeNode(value = "staffDepartments")})
public class AbStaff extends BaseEntity {

    /** 成员编码 */
    @Column(nullable = false, updatable = false)
    @SerialNumber(key = "{companyId}:{staffType}", value = "{staffType}{n}", numberLength = 8)
    @Order(1)
    private String staffCode;

    /** 成员名称 */
    @Column(nullable = false)
    @Order(2)
    private String staffName;

    /** 手机号 */
    @Order(4)
    private String mobile;

    /** 职位 */
    @Order(5)
    private String position;

    /** 性别 */
    @Order(6)
    private Integer gender;

    /** 邮箱 */
    @Order(7)
    private String email;

    /** 是否可用 */
    @Order(8)
    private Boolean enable;

    /** 头像 */
    @Order(9)
    private String avatar;

    /** 成员类型 */
    @Column(nullable = false)
    @Order(10)
    private String staffType;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "staffId")
    @Order(11)
    private List<AbStaffDepartment> staffDepartments;

    /** 公司id */
    @Column(nullable = false, updatable = false)
    @Order(12)
    private String companyId;

    @SuppressWarnings("unused")
    @DomainEvents
    Collection<AbStaff> domainEvents() {
        log.info("事件->成员持久化{}", this);
        return Collections.singletonList(this);
    }
}
